﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using NNanomsg.Protocols;

namespace WPFNanoMsgClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        DispatcherTimer timer;
        static string log = "";
        void UpdateLog(object sender, EventArgs e)
        {
            textBlock_log.Text = log;
        }
        void StartTimer()
        {
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(200);
            timer.Tick += UpdateLog;
            timer.Start();
        }
        public MainWindow()
        {
            InitializeComponent();
            StartTimer();
        }

        bool runFlag = false;
        static string topicstr;
        static string serveraddress;

        public static void DealReceive()
        {
           
        }
        static  void DealReceivedata()
        {
            //await Task.Run({
            //});
            using (var s = new SubscribeSocket())
            {
                //Needs to match the first portion of the message being received.

                //需要订阅多个数据
                //string topicstr = textBox_topic.Text;
                string[] topics = topicstr.Split('#');
                foreach (var singletopin in topics)
                {
                    s.Subscribe(singletopin);
                }


                s.Connect(serveraddress);
                while (true)
                {
                    byte[] b = s.Receive();
                    if (b != null)
                    {
                        Console.WriteLine("Received: " + Encoding.ASCII.GetString(b));
                        if (log.Count() > 500)
                        {
                            log = "";
                        }
                        else
                        {
                            log = "received:" + Encoding.UTF8.GetString(b) + "\n" + log;
                        }
                    }
                    else
                    {
                        Console.WriteLine("x");
                    }
                }
            }
        }

        Thread th = new Thread(DealReceivedata);
        private void button_Click(object sender, RoutedEventArgs e)
        {
            topicstr = textBox_topic.Text;
            serveraddress = textBox_address.Text;
            if(runFlag)
            {
                th.Abort();                
            }
            else
            {
                th.Start();
            }
            runFlag = !runFlag;
                 
        }
    }
}
